import $axios from "@/common/http.js";//请求实例
import { Message } from "element-ui";
import router from "@/router"
// import store from '@/store'


/**
 *  添加商品规格
 * @param {*} data 
 * @returns 
 */
export const getSpecsadd = async (data) => {
    let arr
    if (typeof data.attrs[0] == "string") {
        arr = data.attrs
    } else {
        arr = data.attrs[0].join()
    }
    let { specsname, status } = data
    let rst = await $axios.post('/specsadd', { attrs: arr, specsname, status })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
                // store.commit('shoptions/getSpecslist')
                return true
            }
        })
    } else {
        return false
    }
}


/**
 * 获取分类列表 
 * @param {*} data 
 * @returns 
 */


export const getSpecslist = async (data) => {
    return await $axios.get('/specslist', { params: data })
}


/**
 *  获取总分类分页总数
 * @returns 
 */
export const getSpecscout = async () => {
    return await $axios.get('/specscount',)
}

/**
 * 查找一条数据
 * @param {*} id 
 * @returns 
 */
export const ocneSpecs = async (id) => {
    return await $axios.get('/specsinfo?id=' + id)
}


/**
 * 修改
 * @param {*} data 
 * @returns 
 */
export const editSpecs = async (data) => {
    let arr
    if (typeof data.attrs[0] == "string") {
        arr = data.attrs
    } else {
        arr = data.attrs[0].join()
    }
    let { specsname, status, id } = data
    let rst = await $axios.post('/specsedit', { id, attrs: arr, specsname, status })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
                // store.commit('shoptions/getSpecslist')
                return true
            }
        })
    } else {
        return false
    }
    // await $axios.post('/specsedit', data)
}

export const delSpecs = async (id) => {
    return await $axios.post('/specsdelete', { id })
}